package com.feri.fc.controller;

import com.feri.fc.config.RocketMQConfig;
import com.feri.fc.mq.PayCallback;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 千锋Java,为你而生！
 *
 * @Author: Feri
 * @Date:2024/11/26 14:18
 * @Modified:
 */
@Controller
@RequestMapping("/api/pay")
@Slf4j
public class PaycbController {
    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @PostMapping("alipaucb")
    public void alipayCb(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1.验证签名
        //2.获取回调参数
        String no=request.getParameter("out_trade_no");
        String status=request.getParameter("trade_status");
        log.info("支付宝回调内容，订单号={},订单状态={}",no,status);
        //根据订单号区分是什么支付
        String f=no.substring(0,1);
        String ot="";
        if(f.equals("p")){
            ot="physician";
        }else if(f.equals("o")){
            ot="order";
        }

        //3.mq 异步发送
        rocketMQTemplate.convertAndSend(RocketMQConfig.TOPIC_PAY,new PayCallback(no,status,"支付宝",ot));
        //4.返回 success
        response.getWriter().print("success");

    }
}
